package com.hainatong.shiphelper.module.QSHE;


import android.view.ViewGroup;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentListViewBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.QSHE.adapter.QSHETypeAdapter;
import com.hainatong.shiphelper.module.QSHE.entity.QSHETypeBean;
import com.hainatong.shiphelper.module.QSHE.viewmodel.QSHEViewModel;
import android.view.View;

import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.utils.RecyclerViewHelper;

public class QSHEMgrFragment extends MvvmLazyFragment<FragmentListViewBinding, QSHEViewModel>
{

    private QSHETypeAdapter adapter;

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_list_view;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
        adapter = new QSHETypeAdapter();
        View header = getLayoutInflater().inflate(R.layout.driver_dp10 , (ViewGroup) viewDataBinding.recyclerview.getParent(),false);
        adapter.addHeaderView(header);
        RecyclerViewHelper.addLinerLayoutMgrNoDecoration(viewDataBinding.recyclerview);
        viewDataBinding.recyclerview.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter1, view1, position) -> {
            QSHETypeBean typeBean = adapter.getItem( position );
            ComFragmentActivity.startViewByParam(getActivity(),Constants.VIEW_SMIS_VIEW, GsonUtil.obj2json( typeBean ));
        });
    }

    @Override
    protected void lazyLoad()
    {
        //先查 类型
        viewModel.queryQSHETypeList();
        //类型 处理
        viewModel.getQSHETypeResult().observe( this , result -> {
           if( result.isSuccess() )
           {
                if( result.getData() != null && !result.getData().isEmpty() )
                {
                    adapter.setList( result.getData() );
                }
           }
           else
           {
               ToastUtils.showShort( result.getMsg() );
           }
        } );

    }
}
